/**
 * Created by alice on 2017/2/17 0017
 */
;-function (window) {
    "use strict";
    //TODO 预设变量写这里，全局变量写在base.js 里面 使用 r. 调用
    var ready = {
        layer: null,
    };
    //TODO 初始化的方法写这里面
    var init = function () {
        init_ready();//加载预设变量
        init_event();

        function init_event() {//TODO 初始化页面事件
            var case_panel = $('#case_panel');
            case_panel.append('<p><span>法院：' + (major_case.courtName ? major_case.courtName : '未知') + '</span></p>');
            case_panel.append('<p><span>案件名称：' + (major_case.name ? major_case.name : '未知') + '</span></p>');
            case_panel.append('<p><span>原告：' + (major_case.accuse ? major_case.accuse : '未知' ) + '</span></p>');
            case_panel.append('<p><span>被告：' + (major_case.accused ? major_case.accused : '未知') + '</span></p>');
            case_panel.append('<p><span>案号：' + (major_case.archives ? major_case.archives : '未知') + '</span></p>');
            case_panel.append('<p><span>案件类型：' + (major_case.caseType ? major_case.caseType : '未知') + '</span></p>');
            case_panel.append('<p><span>当事人：' + (major_case.party ? major_case.party : '未知') + '</span></p>');
            case_panel.append('<p><span>案件详情：' + (major_case.summary ? major_case.summary : '未知') + '</span></p>');

            var evidence_panel = $('#evidence_panel');
            var gif_div = evidence_panel.find('.col-md-1').eq(0).remove();
            var rar_div = evidence_panel.find('.col-md-1').eq(0).remove();
            var word_div = evidence_panel.find('.col-md-1').eq(0).remove();
            var file_div = evidence_panel.find('.col-md-1').eq(0).remove();
            $(major_evidence).each(function (index, elem) {
                var temp_div;
                switch (elem.type) {
                    case 'gif' :
                    case 'jpg':
                        temp_div = gif_div.clone();
                        temp_div.find('img').attr('src', elem.path + elem.name);
                        temp_div.find('img').attr('onerror', 'javascript:this.src=\'' + r.base_path + 'assets/img/gif.png\'');
                        break;
                    case 'doc':
                    case 'docx':
                        temp_div = word_div.clone();
                        break;
                    case 'zip':
                    case 'rar':
                        temp_div = rar_div.clone();
                        break;
                    default:
                        temp_div = file_div.clone();
                        break;
                }
                temp_div.find('span').html(elem.name);
                temp_div.on('click',function () {
                    window.location.href = r.base_path+'major_case/download/'+elem.id;
                });
                evidence_panel.append(temp_div);
            });
            if (major_plan) {
                var plan_panel = $('#plan_panel').find('table');
                plan_panel.bootstrapTable({
                    columns: [{
                        field: 'Number',
                        title: '序号',
                        width:'8%',
                        formatter: function (value, row, index) {
                            return index + 1;
                        }
                    }, {
                        field: 'courtRoomName',
                        title: '法庭'
                    }, {
                        field: 'judgeName',
                        title: '法官'
                    }, {
                        field: 'secretaryName',
                        title: '书记员'
                    }, {
                        field: 'startDate',
                        title: '排期日期',
                        formatter: function (value) {
                            var date = new Date(value);
                            return date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate()+' '+date.getHours()+':'+date.getMinutes();
                        }
                    }, {
                        field: 'status',
                        title: '庭审状态',
                        formatter: function (value) {
                            switch (value) {
                                case 0:return'排期';break;
                                case 1:return'正在开庭';break;
                                case 2:return'休庭';break;
                                case 3:return'闭庭';break;
                                case 4:return'归档';break;
                                case 5:return'正在开庭';break;
                                default :return'未知';
                            }
                        }
                    }],
                    showFooter: false,
                    onClickRow: function (row) {
                        Class.load_trial(row.uid);
                    }
                });
                plan_panel.bootstrapTable("hideLoading");
                plan_panel.bootstrapTable('load',major_plan);
                var trial_panel = $('#trial_panel').find('table');
                trial_panel.bootstrapTable({
                    columns: [
                        {
                            field: 'Number',
                            title: '序号',
                            width: '8%',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        }, {
                            field: 'startDate',
                            title: '开庭日期',
                            formatter: function (value) {
                                var date = new Date(value);
                                return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes();
                            }
                        }, {
                            field: 'restDate',
                            title: '闭庭日期',
                            formatter: function (value, row) {
                                if (row.status == 2 && row.restDate) {
                                    var date = new Date(row.restDate);
                                    return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes();
                                } else if (row.endDate) {
                                    var date = new Date(row.endDate);
                                    return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes();
                                }
                            }
                        }, {
                            field: 'status',
                            title: '庭审状态',
                            formatter: function (value) {
                                switch (value) {
                                    case 0:return'排期';break;
                                    case 1:return'正在开庭';break;
                                    case 2:return'休庭';break;
                                    case 3:return'闭庭';break;
                                    case 4:return'归档';break;
                                    default :return'未知';
                                }
                            }
                        }],
                    formatNoMatches:function () {
                        return'请选择排期记录来加载开庭记录。'
                    },
                    showFooter: false,
                    onClickRow: function (row) {
                        if (row.uid) {
                            window.location.href = r.base_path +'major_case/play/'+ row.uid;
                        }
                    }
                });
                trial_panel.bootstrapTable("hideLoading");
            }
            evidence_panel.show();
        }

        function init_ready() {//TODO 初始化预设值
            layui.use('layer', function () {
                ready.layer = layui.layer;
            });

        }
    };
    //TODO 内部调用的方法写这里
    var Class = {
        init: function () {
        },
        load_trial:function (uid) {
            var trial_panel = $('#trial_panel').find('table');
            var data = [];
            $(major_trial).each(function (index, elem) {
                if (elem.planUid==uid) {
                    data.push(elem);
                }
            });
            if (data.length>0) {
                trial_panel.bootstrapTable('load',data);
            }else {
                ready.layer.msg('未能找到相应的开庭记录。');
            }
        }
    };
    init();
    //TODO 对外公开的方法写在这里
    window.p = {};
}(window);